package leetcode;

/**
 * <p></p>
 *
 * @author bin.zhang
 * <p/>
 * Revision History:
 * 2020/09/19, 初始化版本
 * @version 1.0
 **/
public class MyAtoi {


	public int myAtoi(String str) {
		str = str.trim();
		boolean bool = false;
		int rev = 0;
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c == '-' && i == 0) {
				bool = true;
				continue;
			}
			if(c == '+' && i == 0){
				continue;
			}
			if (c >= 48 && c <= 57) {
				int pop = (int) c - (int) '0';
				if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && !bool && pop > 7))  return Integer.MAX_VALUE;
				if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && bool && pop > 8)) return Integer.MIN_VALUE;
				rev = bool ? rev * 10 - pop : rev * 10 + pop;
				continue;
			}
			break;
		}
		return rev;
	}

	public static void main(String[] args) {
		System.out.println(new MyAtoi().myAtoi("-2147483649"));
	}
}
